package com.zly.bscloud.service.impl;

import com.zly.bscloud.dto.OrdersDTO;
import com.zly.bscloud.entity.Orders;
import com.zly.bscloud.mapper.OrdersMapper;
import com.zly.bscloud.service.OrdersService;
import com.zly.bscloud.utils.entity.StatusCode;
import com.zly.bscloud.utils.exception.BpException;
import com.zly.bscloud.utils.utils.BeanHelper;
import com.zly.bscloud.utils.utils.UUIDUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;
import java.util.Map;

/**
 * @Author zhouxiaoyu
 * @Date 2020/3/22 11:10
 * @Version 1.0
 */
@Service
public class OrdersServiceImpl implements OrdersService {

    @Autowired
    private OrdersMapper ordersMapper;

    @Transactional
    public OrdersDTO userReserve(Orders orders){
        orders.setReceived(0);
        orders.setId(UUIDUtils.UUID());
        LocalDateTime dateTime = LocalDateTime.now();
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        orders.setCreate_time(dateTime.format(formatter));

        //插入进数据库
        int count = ordersMapper.insert(orders);
        //只要不为1 则操作失败 抛出异常
        if (count != 1)
            throw new BpException(StatusCode.Failure);
        OrdersDTO ordersDTO = BeanHelper.copyProperties(orders, OrdersDTO.class);
        return ordersDTO;
    }

    /**
     * 获得全部订单的dto信息
     * @return 返回所有订单的dto信息
     */
    @Override
    public List<OrdersDTO> getAllOrders() {

        // 创建Example
        Example example = new Example(Orders.class);
        // 创建Criteria
        Example.Criteria criteria = example.createCriteria();
        // 添加条件
        criteria.andEqualTo("received", 0);
        //example.setOrderByClause("create_time desc");
        List<Orders> ordersList = ordersMapper.selectByExample(example);

        List<OrdersDTO> ordersDTOList = BeanHelper.copyWithCollection(ordersList, OrdersDTO.class);
        return ordersDTOList;
    }

    @Override
    public List<OrdersDTO> getUserOrders(String received,String user_id) {

        // 创建Example
        Example example = new Example(Orders.class);
        // 创建Criteria
        Example.Criteria criteria = example.createCriteria();
        // 添加条件
        criteria.andEqualTo("user_id",user_id).andEqualTo("received", received);

        List<Orders> ordersList = ordersMapper.selectByExample(example);

        List<OrdersDTO> ordersDTOList = BeanHelper.copyWithCollection(ordersList, OrdersDTO.class);
        return ordersDTOList;
    }

    @Override
    public List<OrdersDTO> getUserReceives(String received,String user_id) {

        // 创建Example
        Example example = new Example(Orders.class);
        // 创建Criteria
        Example.Criteria criteria = example.createCriteria();
        // 添加条件
        criteria.andEqualTo("received_userid",user_id).andEqualTo("received", received);

        List<Orders> ordersList = ordersMapper.selectByExample(example);

        List<OrdersDTO> ordersDTOList = BeanHelper.copyWithCollection(ordersList, OrdersDTO.class);
        return ordersDTOList;
    }

    /**
     * 获得订单的dto信息
     * @return 返回订单的dto信息
     */
    @Override
    @Transactional
    public OrdersDTO getOrdersByAId(String id){
        Orders order = ordersMapper.selectByPrimaryKey(id);

        OrdersDTO orderDTO = BeanHelper.copyProperties(order, OrdersDTO.class);
        return orderDTO;
    }

    /**
     * 更改订单的信息
     */
    @Override
    @Transactional
    public void userReserved(Map<String, String> map){

        Orders order = new Orders();
        order.setId(map.get("id"));
        // 首先查出此订单
        Orders selectOne = ordersMapper.selectOne(order);
        selectOne.setReceived_userid(map.get("reserved_userid"));
        selectOne.setReceived(1);
        //再次进行更新
        int count = ordersMapper.updateByPrimaryKey(selectOne);
        // 如果不为1 则抛出异常
        if (count != 1)
            throw new BpException(StatusCode.UserReservedFailure);
    }
}
